package org.zfes.snowier.cloud.zbss.cms.checkFlow.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.zfes.snowier.core.consts.AppBizConst;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;

import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.cms.checkFlow.service.ICmsCheckFlowStepsService;
import org.zfes.snowier.cloud.zbss.cms.checkFlow.service.ICmsCheckHisService;
import org.zfes.snowier.cloud.zbss.cms.checkFlow.service.ICmsCheckProcessService;
import org.zfes.snowier.auth.biz.model.AuthUser;
import org.zfes.snowier.auth.biz.service.IAuthUserService;
import org.zfes.snowier.auth.shiro.util.SecurityUtil;
import org.zfes.snowier.cloud.zbss.cms.checkFlow.dao.CmsCheckProcessMapper;
import org.zfes.snowier.cloud.zbss.cms.checkFlow.model.CmsCheckFlowSteps;
import org.zfes.snowier.cloud.zbss.cms.checkFlow.model.CmsCheckHis;
import org.zfes.snowier.cloud.zbss.cms.checkFlow.model.CmsCheckProcess;
import java.util.stream.Stream;
@Service
public class CmsCheckProcessServiceImpl implements ICmsCheckProcessService{
 	@Autowired
	private CmsCheckProcessMapper cmsCheckProcessMapper;
	@Autowired
	private ICmsCheckFlowStepsService cmsCheckFlowStepsService;
	@Autowired
	private ICmsCheckHisService cmsCheckHisService;
	
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createCmsCheckProcess(CmsCheckProcess cmsCheckProcess) {
		ZBeanUtil.validateBean(cmsCheckProcess);
		cmsCheckProcess.setStatus(Byte.valueOf("1"));
		cmsCheckProcess.setCreateTime(ZDateUtil.newDate());
		cmsCheckProcess.setUpdateTime(ZDateUtil.newDate());
		cmsCheckProcessMapper.insertSelective(cmsCheckProcess);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsCheckProcess> loadCmsCheckProcessById(Long id) {
		if(id!=null){
			return Optional.ofNullable(cmsCheckProcessMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateCmsCheckProcess(CmsCheckProcess cmsCheckProcess) {
		ZBeanUtil.validateBean(cmsCheckProcess);
		cmsCheckProcess.setUpdateTime(ZDateUtil.newDate());
		cmsCheckProcessMapper.updateByPrimaryKeySelective(cmsCheckProcess);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteCmsCheckProcess(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		cmsCheckProcessMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsCheckProcessStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsCheckProcessMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadCmsCheckProcessDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(cmsCheckProcessMapper.selectListCount(pm), cmsCheckProcessMapper.selectMapListPage(pm));
		return ds;
	}
	
}